﻿using System;
using System.ComponentModel;
using System.Windows;
using System.Windows.Input;
using GuideServices.Service.IService;
using Services.GetData.Entity;
using Utilities.Configuration;
using Microsoft.Phone.Controls;

namespace EnglishStudy.ViewModel
{
    public class SignUpViewModel : INotifyPropertyChanged
    {
        private string userName;

        private bool isHitTest;
        private double opacity;
        private string password;
        private string confirmPassword;
        private Visibility showAlert;
        private Visibility showProgressBar;

        public SignUpViewModel()
        {
            ShowProgressBar = Visibility.Collapsed;
            IsHitTest = true;
            Opacity = 1;
        }

        public string Password
        {
            get { return password; }
            set
            {
                password = value;
                NotifyPropertyChanged("Password");
            }
        }

        public string ConfirmPassword
        {
            get { return confirmPassword; }
            set
            {
                confirmPassword = value;
                NotifyPropertyChanged("ConfirmPassword");
            }
        }

        public string UserName
        {
            get { return userName; }
            set
            {
                userName = value;
                NotifyPropertyChanged("UserName");
            }
        }

        public Visibility ShowAlert
        {
            get { return showAlert; }
            set
            {
                showAlert = value;
                NotifyPropertyChanged("ShowAlert");
            }
        }

        public bool IsHitTest
        {
            get { return isHitTest; }
            set
            {
                isHitTest = value;
                NotifyPropertyChanged("IsHitTest");
            }
        }

        public double Opacity
        {
            get { return opacity; }
            set 
            { 
                opacity = value;
                NotifyPropertyChanged("Opacity");
            }
        }

        public Visibility ShowProgressBar
        {
            get { return showProgressBar; }
            set 
            { 
                showProgressBar = value;
                NotifyPropertyChanged("ShowProgressBar");
            }
        }

        #region INotifyPropertyChanged Members

        public event PropertyChangedEventHandler PropertyChanged;

        // Used to notify Silverlight that a property has changed.
        private void NotifyPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }
        #endregion

        #region Commands

        public ICommand GetUserNameCommand
        {
            get { return new DelegateCommand(GetUserName, CanExecute); }
        }

        public ICommand GetPasswordCommand
        {
            get { return new DelegateCommand(GetPassword, CanExecute); }
        }

        public ICommand GetConfirmPasswordCommand
        {
            get { return new DelegateCommand(GetConfirmPassword, CanExecute); }
        }

        public ICommand RegisterCommand
        {
            get { return new DelegateCommand(Register, CanExecute); }
        }

        public void GetUserName(object parameter)
        {
            UserName = parameter.ToString();
        }

        public void GetPassword(object parameter)
        {
            Password = parameter.ToString();
        }

        private void GetConfirmPassword(object parameter)
        {
            ConfirmPassword = parameter.ToString();
        }

        private bool CanExecute(object arg)
        {
            return true;
        }
        
        /// <summary>
        ///     Handle register button click event.
        /// </summary>
        /// <param name="obj">obj = null.</param>
        private async void Register(object obj)
        {
            if (string.IsNullOrWhiteSpace(UserName))
            {
                MessageBox.Show("User name is empty.");
                return;
            }
            if (string.IsNullOrWhiteSpace(Password))
            {
                MessageBox.Show("Password is empty.");
                return;
            }
            if (Password != ConfirmPassword)
            {
                MessageBox.Show("Passwords do not match.");
                return;
            }
            var guideService = (IWebService)ApplicationContext.Instance.GetInstance(typeof(IWebService));
            var user = new RequestUser()
            {
                //Address = "Bang California",
                //Avatar = "Hinh dep.jpg",
                //Birthday = "12/12/2012",
                //Email = "mam@gmail.com",
                //FacebookID = string.Empty,
                //FaceName = string.Empty,
                //FullName = "Hoang van Ten",
                //Gender = true,
                //IdCountry = "2",
                Password = Password,
                Username = UserName,
                //Zipcode = "123"
            };
            var login = await guideService.SignUp(user);
            if (login != null && login.status.status == "success")
            {
                ((PhoneApplicationFrame)Application.Current.RootVisual).Navigate(
                        new Uri("/MainPage.Xaml", UriKind.Relative));
            }
            else if (login != null)
            {
                MessageBox.Show(login.status.messages, "Error", MessageBoxButton.OK);
            }
        }
        #endregion
    }
}
